<?php
namespace app\common\netfilter;

use app\common\definition\InternetProtocolsDefinition;

class LinuxRule
{
    public $protocols;//协议
    public $rulehead = '';//规则头部
    private $haveMacAddr ;//是否输出mac地址
    public $direction ;//流量方向
    /**
     * @name: 规则状态
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-12 16:14:15
     */
    public function status ()
    {
        $status_s = '-A' ;

        return $status_s;
    }

    /**
     * @name: 流量方向
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-12 16:18:30
     */
    public function direction ($direction)
    {
        $return = $direction == 1 ? 'OUTPUT' : 'INPUT';

        return $return;
    }

    /**
     * @name: ip地址
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-12 16:14:41
     */
    public function ip ($src_ip, $dest_ip)
    {
        $return = '';
        if ($src_ip != 'any')
        {
            $return .= strlen($src_ip) > 15 ? ' -m iprange --src-range ' . $src_ip : ' -s ' . $src_ip . '/32' ; 
        }
        
        if ($dest_ip != 'any')
        {
            $return .= strlen($dest_ip) > 15 ? ' -m iprange --dst-range ' . $dest_ip : ' -d ' . $dest_ip . '/32' ; 
        }
        

        return $return;
    }

    /**
     * @name: 协议
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-12 16:15:32
     */
    public function protocols ()
    {
        if($this->protocols == InternetProtocolsDefinition::ALL_PROTOCOL) 
        {
            return '';
        }

        $return = '-p ';
        $return .= $this->protocols == InternetProtocolsDefinition::TCP_PROTOCOL ? 'tcp' : 'udp';

        return $return;
    }
    
    /**
     * @name: 端口
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-12 16:15:14
     */
    public function port ($src_port, $dest_port)
    {
        
        $return = '';

        if($this->protocols == InternetProtocolsDefinition::ALL_PROTOCOL || ($src_port == 'any' && $dest_port == 'any')) 
        {
            return $return;
        }

        $str = ' -m multiport ';
        if ($src_port != 'any')
        {
            $return .= $str  . ' --sports ' . $src_port ;
        }
        
        if ($dest_port != 'any')
        {
            $return .= $str  . ' --dports ' . $dest_port ;
        }
        

        return $return;
    }

    /**
     * @name: 是否输出mac地址
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-31 10:07:58
     */
    public function have_mac_addr ($have_mac_addr)
    {
        $this->haveMacAddr = $have_mac_addr;
    }
    /**
     * @name: mac 地址
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-12 17:03:55
     */
    public function mac_addr ($mac = '')
    {
        $return = '';

        if (empty($mac) || $this->haveMacAddr == 0)
        {
            return $return;
        }

        $return = '-m mac --mac-source ' . strtoupper($mac);

        return $return;
    }

    /**
     * @name: 处理方式
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-13 15:49:10
     */
    public function process ($process)
    {
        $return = '-j ';
        $return .= $process == 1 ? 'ACCEPT' : 'DROP';

        return $return;
    }
}